package org.analyzer.custom;

import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.core.LowerCaseFilter;
import org.apache.lucene.analysis.core.StopFilter;
import org.apache.lucene.analysis.standard.StandardFilter;
import org.apache.lucene.analysis.standard.StandardTokenizer;
import org.apache.lucene.analysis.util.CharArraySet;
import org.apache.lucene.analysis.util.StopwordAnalyzerBase;
import org.apache.lucene.util.Version;

import java.io.IOException;
import java.io.Reader;

public class NoStopwordAnalyrzer extends StopwordAnalyzerBase{

	  /** Default maximum allowed token length */
	  public static final int DEFAULT_MAX_TOKEN_LENGTH = 255;

	  private int maxTokenLength = DEFAULT_MAX_TOKEN_LENGTH;

	  /** An unmodifiable set containing some common English words that are usually not
	  useful for searching. */
	  public static final CharArraySet STOP_WORDS_SET =  CharArraySet.EMPTY_SET; 

	  /** Builds an analyzer with the given stop words.
	   * @param matchVersion Lucene version to match See {@link
	   * <a href="#version">above</a>}
	   * @param stopWords stop words */
	  public NoStopwordAnalyrzer(Version matchVersion, CharArraySet stopWords) {
	    super(matchVersion, stopWords);
	  }

	  /** Builds an analyzer with the default stop words ({@link
	   * #STOP_WORDS_SET}).
	   * @param matchVersion Lucene version to match See {@link
	   * <a href="#version">above</a>}
	   */
	  public NoStopwordAnalyrzer(Version matchVersion) {
	    this(matchVersion, STOP_WORDS_SET);
	  }

	  /** Builds an analyzer with the stop words from the given reader.
	   * @see WordlistLoader#getWordSet(java.io.Reader, org.apache.lucene.util.Version)
	   * @param matchVersion Lucene version to match See {@link
	   * <a href="#version">above</a>}
	   * @param stopwords Reader to read stop words from */
	  public NoStopwordAnalyrzer(Version matchVersion, Reader stopwords) throws IOException {
	    this(matchVersion, loadStopwordSet(stopwords, matchVersion));
	  }

	  /**
	   * Set maximum allowed token length.  If a token is seen
	   * that exceeds this length then it is discarded.  This
	   * setting only takes effect the next time tokenStream or
	   * tokenStream is called.
	   */
	  public void setMaxTokenLength(int length) {
	    maxTokenLength = length;
	  }
	    
	  /**
	   * @see #setMaxTokenLength
	   */
	  public int getMaxTokenLength() {
	    return maxTokenLength;
	  }

	  @Override
	  protected TokenStreamComponents createComponents(final String fieldName, final Reader reader) {
//		  Version matchVersion = getVersion();
	    final StandardTokenizer src = new StandardTokenizer( reader);
	    src.setMaxTokenLength(maxTokenLength);
	    TokenStream tok = new StandardFilter(src);
	    tok = new LowerCaseFilter( tok);
	    tok = new StopFilter(tok, stopwords);
	    return new TokenStreamComponents(src, tok) {
	      @Override
	      protected void setReader(final Reader reader) throws IOException {
	        src.setMaxTokenLength(NoStopwordAnalyrzer.this.maxTokenLength);
	        super.setReader(reader);
	      }
	    };
	  }
}
